package com.jbit.dm212.service.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.dm212.entity.dto.DmCinemaSeatArrayDTO;
import com.jbit.dm212.entity.dto.DmCinemaSeatPriceDTO;
import com.jbit.dm212.entity.dto.DmSchedulerDTO;
import com.jbit.dm212.entity.po.DmScheduler;
import com.jbit.dm212.entity.po.DmSchedulerSeat;
import com.jbit.dm212.entity.po.DmSchedulerSeatPrice;
import com.jbit.dm212.mapper.DmSchedulerMapper;
import com.jbit.dm212.service.DmSchedulerService;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service
public class DmSchedulerServiceImpl implements DmSchedulerService {
    @Resource
    private DmSchedulerMapper dmSchedulerMapper;

    @Override
    public List<DmSchedulerDTO> getSchedulersByItemId(Integer itemId) {
        MPJLambdaWrapper<DmScheduler> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(
                DmScheduler::getId,
                DmScheduler::getTitle,
                DmScheduler::getItemId,
                DmScheduler::getCinemaId,
                DmScheduler::getStartTime,
                DmScheduler::getEndTime
        );
        lambdaWrapper.eq(DmScheduler::getItemId,itemId);
        return dmSchedulerMapper.selectJoinList(DmSchedulerDTO.class,lambdaWrapper);
    }

    @Override
    public DmCinemaSeatArrayDTO getSeatArrayByScheduleId(DmScheduler scheduler) {
        MPJLambdaWrapper<DmScheduler> lambdaWrapper = new MPJLambdaWrapper();
        lambdaWrapper.select(DmScheduler::getCinemaId);
        lambdaWrapper.selectAs(DmScheduler::getId,"scheduleId");
        lambdaWrapper.eq(DmScheduler::getId,scheduler.getId());

        //关联座位价格表
        lambdaWrapper.selectCollection(DmSchedulerSeatPrice.class,DmCinemaSeatArrayDTO::getSeatPriceList, map->
                map
                        .result(DmSchedulerSeatPrice::getPrice)
                        .result(DmSchedulerSeatPrice::getAreaLevel)
        );
        lambdaWrapper.innerJoin(DmSchedulerSeatPrice.class,DmSchedulerSeatPrice::getScheduleId,DmScheduler::getId);

        //关联座位详情表
        lambdaWrapper.selectCollection(DmSchedulerSeat.class,DmCinemaSeatArrayDTO::getSeatInfoList,map->{
            return map.id(DmSchedulerSeat::getId)
                    .result(DmSchedulerSeat::getX)
                    .result(DmSchedulerSeat::getAreaLevel)
                    .result(DmSchedulerSeat::getY)
                    .result(DmSchedulerSeat::getSort)
                    .result(DmSchedulerSeat::getStatus);
        });
        lambdaWrapper.innerJoin(DmSchedulerSeat.class,DmSchedulerSeat::getScheduleId,DmScheduler::getId);


        DmCinemaSeatArrayDTO seatArrayDTO = dmSchedulerMapper.selectJoinOne(DmCinemaSeatArrayDTO.class,lambdaWrapper);
        //将areaLevel 修改为 areaLevelName
        seatArrayDTO.getSeatPriceList().forEach(seatPriceDTO -> changeAreaLevelNameByAreaLevel(seatPriceDTO));
        seatArrayDTO.getSeatInfoList().forEach(seatInfo->seatInfo.setCinemaId(seatArrayDTO.getCinemaId()));
        return seatArrayDTO;
    }

    public void changeAreaLevelNameByAreaLevel(DmCinemaSeatPriceDTO seatPriceDTO){
        switch (seatPriceDTO.getAreaLevel()){
            case 1:
                seatPriceDTO.setAreaLevelName("A");
                break;
            case 2:
                seatPriceDTO.setAreaLevelName("B");
                break;
            case 3:
                seatPriceDTO.setAreaLevelName("C");
                break;
        }
    }
}
